package first.niuke.huawei.mediumDifficulty;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * @ClassName HJ102
 * @description:
 * @author: 荣燊
 * @create: 2021-07-13 09:54
 **/
public class HJ102 {
    // 字符统计,三课※,如何新建自动排序的

    public static void main(String[] args) {
        String s = "aaassdsda";

        // 这样写和hashMap完全不一样了,按照字符大小进行排序
        Map<Character ,Integer> map = new TreeMap<Character ,Integer>(
                new Comparator<Character>() {
                    @Override
                    public int compare(Character o1, Character o2) {
                        return o1.compareTo(o2);
                    }
                }
//                new Comparable<Character>() {
//
//                    @Override
//                    public int compareTo(Character o) {
//                        return 0;
//                    }
//                }
        );

//        HashMap<Character, Integer> map = new HashMap<>();
        for (int i=0; i<s.length(); i++) {
            if (map.containsKey(s.charAt(i))) {
                map.put(s.charAt(i), map.get(s.charAt(i))+1);
            } else {
                map.put(s.charAt(i), 1);
            }
        }

        // 再将map集合中的key值赋值给ArrayList，
        ArrayList<Map.Entry<Character, Integer>> entries = new ArrayList<>(map.entrySet());
        // 按照map集合的value进行排序
        Collections.sort(entries, new Comparator<Map.Entry<Character, Integer>>() {
            @Override
            public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
                // 比较他们的值
                return o1.getValue().compareTo(o2.getValue());
            }
        });

        System.out.println(entries);
    }
}
